package petertel.pbx.conf;

import petertel.pbx.conf.sip.Device.Property;
import petertel.pbx.controller.EnumConf;

public class NameValuePair extends ConfigItem {

	private String name;
	private String value;

	@Override
	public String toString() {
		String ret = "";
		ret += "[" + getLineno() + "][" + getFlag() + "] " + getName() + " = "
				+ getValue();
		ret += "\n";
		return ret;
	}

	public String getValueEditor() {
		StringBuilder sb = new StringBuilder();
		if (name.equals(Property.context.name())
				|| name.equals(Property.udpbindaddr.name())
				|| name.equals(Property.transport.name())
				|| name.equals(Property.tcpbindaddr.name())
				|| name.equals(Property.defaultuser.name())) {
			sb.append("<input type='text' name='propertyValue' ");
			sb.append("value='");
			sb.append(value==null ? "" : value);
			sb.append("' onchange='save(this.form);' />");
		} else {
			for (EnumConf.YesNo yn : EnumConf.YesNo.values()) {
				sb.append("<input type='radio' name='");
				sb.append("propertyValue' ");
				sb.append("value='");
				sb.append(yn);
				sb.append("' ");
				if(value==null&&yn==EnumConf.YesNo.yes){
					sb.append("checked ");
				}else if(value!=null&&value.equals(yn.toString())) {
					sb.append("checked ");
				}
				sb.append("onchange='save(this.form);' />");
				if (yn == EnumConf.YesNo.yes) {
					sb.append("是 ");
				} else {
					sb.append("否 ");
				}
			}
		}
		return sb.toString();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Override
	public String write() {
		return name + " = " + value;
	}

}
